package multi_thread.master.executors;

import java.time.LocalDateTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Cheng Jun
 * Description: Creates an Executor that uses a single worker thread operating off an unbounded queue
 * singleThreadExecutor 相当于只有一个线程的线程池，实际使用场景很少。
 * 缺点：workQueue 参数是 LinkedBlockingQueue，无界的队列有可能会引起 OOM
 * @version 1.0
 * @date 2022/5/19 16:09
 * @see Executors#newSingleThreadExecutor()
 */
public class Executor_SingleThreadPool {

    public static void main(String[] args) {
        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {
            Runnable task = () -> System.out.println(Thread.currentThread().getName() + " 时间：" + LocalDateTime.now());
            singleThreadExecutor.execute(task);
        }
        singleThreadExecutor.shutdown();
    }
}
